﻿Imports System.Windows.Forms
Imports System.Drawing

Public Class frmGestionarPort
    Private dsPortafDispo As New DataSet
    Private dtPortafDispo As New DataTable
    Private dsPortafAsocia As New DataSet
    Private dtPortafAsocia As New DataTable
    Private dsPortaf As New DataSet
    Private dtPortaf As New DataTable
    Private dsTitulo As New DataSet
    Private dtTitulo As New DataTable
    Private IndicePortaf As Integer
    Private tipo As Integer

    Private Sub cmdGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Valores As New ArrayList

        IndicePortaf = dsPortaf.Tables(0).Rows.Count

        cmdAgregar.Enabled = True
        cmdGrabar.Enabled = False
        cmdCancelar.Enabled = False

        MessageBox.Show("Grabación realizada con éxito", TitMsj, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub frmGestionarPort_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Armar_PortafolioGrilla()
        Armar_PortafolioDispoGrilla()

        Armar_TituloGrilla()

        cmdCancelar.Enabled = False
        cmdGrabar.Enabled = False
    End Sub

    Private Sub Armar_PortafolioGrilla()

        Dim dr, dr2, dr3 As DataRow

        dtPortaf.Columns.Add("Código")
        dtPortaf.Columns.Add("Nombre")

        dr = dtPortaf.NewRow
        dr2 = dtPortaf.NewRow
        dr3 = dtPortaf.NewRow

        dr(0) = "CN - GIN"
        dr(1) = "Cartera de Negociación GIN"

        dtPortaf.Rows.Add(dr)

        dr2(0) = "CN - SBS"
        dr2(1) = "Cartera de Negociación SBS"

        dtPortaf.Rows.Add(dr2)

        dr3(0) = "PLIMITES"
        dr3(1) = "Portafolio Total - Límites"

        dtPortaf.Rows.Add(dr3)

        dsPortaf.Tables.Add(dtPortaf)

        dgPortafolios.DataSource = dsPortaf.Tables(0)

    End Sub

    Private Sub cmdAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        cmdGrabar.Enabled = True
        cmdAgregar.Enabled = False
    End Sub

    Private Sub Armar_PortafolioDispoGrilla()
        Dim dr, dr2, dr3 As DataRow

        dtPortafDispo.Columns.Add("Codigo")
        dtPortafDispo.Columns.Add("Nombre")


        dr = dtPortafDispo.NewRow
        dr2 = dtPortafDispo.NewRow
        dr3 = dtPortafDispo.NewRow

        dr(0) = "PFWDD"
        dr(1) = "Portafolio Forward Otras Divisas"


        dtPortafDispo.Rows.Add(dr)

        dr2(0) = "PFWDL"
        dr2(1) = "Portafolio Forward Limites"


        dtPortafDispo.Rows.Add(dr2)

        dr3(0) = "PGICCH"
        dr3(1) = "Gi-Portafolio Swap Moneda de Coberturas"


        dtPortafDispo.Rows.Add(dr3)

        dsPortafDispo.Tables.Add(dtPortafDispo)

    End Sub

    Private Sub cmdPasarIzqTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim i As Integer

        For i = 0 To dsPortafDispo.Tables(0).Rows.Count - 1
            dsPortafAsocia.Tables(0).ImportRow(dsPortafDispo.Tables(0).Rows(i))
        Next

        dsPortafDispo.Clear()
    End Sub

    Private Sub cmdPasarDerTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim i As Integer


        For i = 0 To dsPortafAsocia.Tables(0).Rows.Count - 1
            dsPortafDispo.Tables(0).ImportRow(dsPortafAsocia.Tables(0).Rows(i))
        Next

        dsPortafAsocia.Clear()
    End Sub

    Private Sub Armar_TituloGrilla()

        dtTitulo.Columns.Add("Portafolio")
        dtTitulo.Columns.Add("Tipo Instrumento")
        dtTitulo.Columns.Add("Num. Titulo")
        dtTitulo.Columns.Add("Tipo Titulo")
        dtTitulo.Columns.Add("Codigo Emision")
        dtTitulo.Columns.Add("Negociabilidad")
        dtTitulo.Columns.Add("Bursatilidad")

        dsTitulo.Tables.Add(dtTitulo)

        dgTitulos.DataSource = dsTitulo.Tables(0)

    End Sub

    Private Sub dgPortafAsociados_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Dim Colum, Fila As Integer
        Dim Valores As New ArrayList
        Dim i As Integer

        Colum = e.ColumnIndex
        Fila = e.RowIndex
        If Colum < 0 Or Fila < 0 Then
            Exit Sub
        End If

        dsTitulo.Tables(0).Clear()

        For i = 43 To 45


            Valores.Add("RENTA F.")
            Valores.Add(i)
            Valores.Add("TTFWDFC")
            Valores.Add("FVUSD101")
            Valores.Add("NO NEGOCIABLE")
            Valores.Add("")

            dsTitulo.Tables(0).Rows.Add(Valores.ToArray)

            Valores.Clear()

        Next

    End Sub

    Private Sub dgPortafolios_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgPortafolios.CellMouseClick
        Dim Colum, Fila As Integer
        Dim grid As DataGridView = sender
        Dim pt As Point = grid.PointToClient(Control.MousePosition)

        tipo = 1

        If e.Button = Windows.Forms.MouseButtons.Right Then
            Colum = e.ColumnIndex
            Fila = e.RowIndex
            If Colum < 0 Or Fila < 0 Then
                Exit Sub
            End If

            With Me.dgPortafolios
                Dim Hitest As DataGridView.HitTestInfo = .HitTest(e.X, e.Y)
                If Hitest.Type - 1 = DataGridViewHitTestType.Cell Then
                    .CurrentCell = .Rows(Fila).Cells(Colum)
                End If
            End With


            If dgPortafolios.AllowUserToAddRows = True Then

                If dgPortafolios.Rows.Count > 1 Then

                    Me.ContextMenuStrip1.Show(dgPortafolios, pt.X, pt.Y)

                End If

            Else

                If dgPortafolios.Rows.Count > 0 Then

                    Me.ContextMenuStrip1.Show(dgPortafolios, pt.X, pt.Y)

                End If

            End If

        End If
    End Sub

    Private Sub DataGridView1_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
        Dim Colum, Fila As Integer
        Dim grid As DataGridView = sender
        Dim pt As Point = grid.PointToClient(Control.MousePosition)

        tipo = 2

        If e.Button = Windows.Forms.MouseButtons.Right Then
            Colum = e.ColumnIndex
            Fila = e.RowIndex
            If Colum < 0 Or Fila < 0 Then
                Exit Sub
            End If

            With Me.DataGridView1
                Dim Hitest As DataGridView.HitTestInfo = .HitTest(e.X, e.Y)
                If Hitest.Type - 1 = DataGridViewHitTestType.Cell Then
                    .CurrentCell = .Rows(Fila).Cells(Colum)
                End If
            End With

            If DataGridView1.AllowUserToAddRows = True Then

                If DataGridView1.Rows.Count > 1 Then

                    Me.ContextMenuStrip1.Show(DataGridView1, pt.X, pt.Y)

                End If

            Else

                If DataGridView1.Rows.Count > 0 Then

                    Me.ContextMenuStrip1.Show(DataGridView1, pt.X, pt.Y)

                End If

            End If

        End If
    End Sub

    Private Sub dgTitulos_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgTitulos.CellMouseClick
        Dim Colum, Fila As Integer
        Dim grid As DataGridView = sender
        Dim pt As Point = grid.PointToClient(Control.MousePosition)

        tipo = 3

        If e.Button = Windows.Forms.MouseButtons.Right Then
            Colum = e.ColumnIndex
            Fila = e.RowIndex
            If Colum < 0 Or Fila < 0 Then
                Exit Sub
            End If

            With Me.dgTitulos
                Dim Hitest As DataGridView.HitTestInfo = .HitTest(e.X, e.Y)
                If Hitest.Type - 1 = DataGridViewHitTestType.Cell Then
                    .CurrentCell = .Rows(Fila).Cells(Colum)
                End If
            End With

            If dgTitulos.AllowUserToAddRows = True Then

                If dgTitulos.Rows.Count > 1 Then

                    Me.ContextMenuStrip1.Show(dgTitulos, pt.X, pt.Y)

                End If

            Else

                If dgTitulos.Rows.Count > 0 Then

                    Me.ContextMenuStrip1.Show(dgTitulos, pt.X, pt.Y)

                End If

            End If

        End If
    End Sub

    Private Sub ExportarVolatilidadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportarVolatilidadToolStripMenuItem.Click
        Cursor = Cursors.WaitCursor

        If tipo = 1 Then

            Exportar_Excel(dgPortafolios)

        ElseIf tipo = 2 Then

            Exportar_Excel(DataGridView1)

        ElseIf tipo = 3 Then

            Exportar_Excel(dgTitulos)

        End If

        Cursor = Cursors.Default
    End Sub

End Class